<?php

namespace App\Livewire;

use App\Models\Tenant;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Stancl\Tenancy\Database\Models\Domain;

class DomainListComponent extends Component
{
    #[Validate('required|min:3|unique:domains,domain')]
    public $domainname = '';

    public function render()
    {
        $domains = Domain::all();
        return view('livewire.domain-list-component', [
            'domains' => $domains
        ]);
    }

    public function delete(Domain $domain)
    {
        $domain->tenant->delete();

        $domain->delete();
    }

    public function save()
    {
        $this->validate();

        $tenant = Tenant::create([
            'plan' => 'free',
            'tenancy_db_name' => $this->domainname
        ]);

        $tenant->domains()->create([
            'domain' => $this->domainname,
        ]);

    }

}
